/*
 * MouseIdentAboutBox.java
 */

package mouseident;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;

public class SaveDialog extends javax.swing.JDialog {

    Result result;

    public SaveDialog(java.awt.Frame parent, Result toSave) {
        super(parent);
        initComponents();
        getRootPane().setDefaultButton(SendButton);
        this.result = toSave;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        inputTypeGroup = new javax.swing.ButtonGroup();
        experienceGroup = new javax.swing.ButtonGroup();
        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        AgeLabel = new javax.swing.JLabel();
        AgeTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        MouseRB = new javax.swing.JRadioButton();
        TouchpadRB = new javax.swing.JRadioButton();
        otherRB = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        littleRB = new javax.swing.JRadioButton();
        averageRB = new javax.swing.JRadioButton();
        highRB = new javax.swing.JRadioButton();
        jLabel3 = new javax.swing.JLabel();
        AfricaRB = new javax.swing.JRadioButton();
        AsiaRB = new javax.swing.JRadioButton();
        EuropeRB = new javax.swing.JRadioButton();
        NorthAmericaRB = new javax.swing.JRadioButton();
        OceaniaRB = new javax.swing.JRadioButton();
        SouthAmericaRB = new javax.swing.JRadioButton();
        CancelButton = new javax.swing.JButton();
        SendButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(mouseident.MouseIdentApp.class).getContext().getResourceMap(SaveDialog.class);
        setTitle(resourceMap.getString("title")); // NOI18N
        setModal(true);
        setName("aboutBox"); // NOI18N
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        AgeLabel.setText(resourceMap.getString("AgeLabel.text")); // NOI18N
        AgeLabel.setName("AgeLabel"); // NOI18N

        AgeTextField.setName("AgeTextField"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        inputTypeGroup.add(MouseRB);
        MouseRB.setText(resourceMap.getString("MouseRB.text")); // NOI18N
        MouseRB.setName("MouseRB"); // NOI18N

        inputTypeGroup.add(TouchpadRB);
        TouchpadRB.setText(resourceMap.getString("TouchpadRB.text")); // NOI18N
        TouchpadRB.setName("TouchpadRB"); // NOI18N

        inputTypeGroup.add(otherRB);
        otherRB.setText(resourceMap.getString("otherRB.text")); // NOI18N
        otherRB.setName("otherRB"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        experienceGroup.add(littleRB);
        littleRB.setText(resourceMap.getString("littleRB.text")); // NOI18N
        littleRB.setName("littleRB"); // NOI18N

        experienceGroup.add(averageRB);
        averageRB.setText(resourceMap.getString("averageRB.text")); // NOI18N
        averageRB.setName("averageRB"); // NOI18N

        experienceGroup.add(highRB);
        highRB.setText(resourceMap.getString("highRB.text")); // NOI18N
        highRB.setName("highRB"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        buttonGroup1.add(AfricaRB);
        AfricaRB.setText(resourceMap.getString("AfricaRB.text")); // NOI18N
        AfricaRB.setName("AfricaRB"); // NOI18N

        buttonGroup1.add(AsiaRB);
        AsiaRB.setText(resourceMap.getString("AsiaRB.text")); // NOI18N
        AsiaRB.setName("AsiaRB"); // NOI18N

        buttonGroup1.add(EuropeRB);
        EuropeRB.setText(resourceMap.getString("EuropeRB.text")); // NOI18N
        EuropeRB.setName("EuropeRB"); // NOI18N

        buttonGroup1.add(NorthAmericaRB);
        NorthAmericaRB.setText(resourceMap.getString("NorthAmericaRB.text")); // NOI18N
        NorthAmericaRB.setName("NorthAmericaRB"); // NOI18N

        buttonGroup1.add(OceaniaRB);
        OceaniaRB.setText(resourceMap.getString("OceaniaRB.text")); // NOI18N
        OceaniaRB.setName("OceaniaRB"); // NOI18N

        buttonGroup1.add(SouthAmericaRB);
        SouthAmericaRB.setText(resourceMap.getString("SouthAmericaRB.text")); // NOI18N
        SouthAmericaRB.setName("SouthAmericaRB"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(36, 36, 36)
                                .addComponent(MouseRB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(TouchpadRB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(otherRB))
                            .addComponent(jLabel2)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(101, 101, 101)
                                .addComponent(littleRB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(averageRB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(highRB))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(AgeLabel)
                                .addGap(18, 18, 18)
                                .addComponent(AgeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel3)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(109, 109, 109)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(NorthAmericaRB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(OceaniaRB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(SouthAmericaRB))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(AfricaRB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(AsiaRB)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(EuropeRB)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(MouseRB)
                    .addComponent(TouchpadRB)
                    .addComponent(otherRB))
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(littleRB)
                    .addComponent(averageRB)
                    .addComponent(highRB))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(AgeLabel)
                    .addComponent(AgeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(AfricaRB)
                    .addComponent(AsiaRB)
                    .addComponent(EuropeRB))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(NorthAmericaRB)
                    .addComponent(OceaniaRB)
                    .addComponent(SouthAmericaRB))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        CancelButton.setText(resourceMap.getString("CancelButton.text")); // NOI18N
        CancelButton.setName("CancelButton"); // NOI18N
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });

        SendButton.setText(resourceMap.getString("SendButton.text")); // NOI18N
        SendButton.setName("SendButton"); // NOI18N
        SendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SendButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(262, Short.MAX_VALUE)
                .addComponent(SendButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(CancelButton)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CancelButton)
                    .addComponent(SendButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void SendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SendButtonActionPerformed
        try {
        result.setAge(Integer.valueOf(AgeTextField.getText()));
        } catch (NumberFormatException ex) {
            result.setAge(0);
        }
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String jsonResult = gson.toJson(result);
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://mamey.my1.cc/ml/index.php");
        try {
            post.setEntity(new StringEntity(jsonResult,"application/json",HTTP.UTF_8));


        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }

        try {
            HttpResponse httpResponse = client.execute(post);
            HttpEntity responseEntity = httpResponse.getEntity();

            BufferedReader reader = new BufferedReader(new InputStreamReader(responseEntity.getContent()));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            System.out.println(sb.toString());
            responseEntity.getContent().close();
        } catch (IOException ex) {

        }
        this.dispose();

    }//GEN-LAST:event_SendButtonActionPerformed

    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_CancelButtonActionPerformed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton AfricaRB;
    private javax.swing.JLabel AgeLabel;
    private javax.swing.JTextField AgeTextField;
    private javax.swing.JRadioButton AsiaRB;
    private javax.swing.JButton CancelButton;
    private javax.swing.JRadioButton EuropeRB;
    private javax.swing.JRadioButton MouseRB;
    private javax.swing.JRadioButton NorthAmericaRB;
    private javax.swing.JRadioButton OceaniaRB;
    private javax.swing.JButton SendButton;
    private javax.swing.JRadioButton SouthAmericaRB;
    private javax.swing.JRadioButton TouchpadRB;
    private javax.swing.JRadioButton averageRB;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup experienceGroup;
    private javax.swing.JRadioButton highRB;
    private javax.swing.ButtonGroup inputTypeGroup;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton littleRB;
    private javax.swing.JRadioButton otherRB;
    // End of variables declaration//GEN-END:variables
    
}
